<template>
  <div>
    <MaskContainer ref="MaskContainer" :main-style="maskContainerStyle" :has-btn="false" :has-close="true" title="案例列表">
      <div class="remark-container">
        <div v-loading="listLoading" class="remark-table-container">
          <el-table
            :data="caseList"
            :border="true"
            max-height="390"
            header-row-class-name="remark-tabel-header"
            header-cell-class-name="remark-tabel-header-cell"
            cell-class-name="remark-tabel-cell"
            style="width: 100%;border-radius: 8px">
            <el-table-column align="center" label="日期" width="150">
              <template slot-scope="scope">
                <div class="tabel-content">{{ scope.row.time | standardFormatNoHour }}</div>
              </template>
            </el-table-column>
            <el-table-column align="left" label="品牌" width="120">
              <template slot-scope="scope">
                <div v-if="!scope.row._isEdit" class="tabel-content">
                  <ToolTip v-if="scope.row.company && scope.row.company.length > 20" :content="scope.row.company">
                    <span class="content">{{ scope.row.company.substr(0, 20) }}...</span>
                  </ToolTip>
                  <span v-else>{{ scope.row.company || '--' }}</span>
                </div>
                <div v-if="scope.row._isEdit" class="tabel-input">
                  <!-- <input v-model="scope.row.editCompany" maxlength="50" type="text" > -->
                  <textarea v-model="scope.row.editCompany" maxlength="50" rows="4"/>
                </div>
              </template>
            </el-table-column>
            <el-table-column align="left" label="案例内容" width="174">
              <template slot-scope="scope">
                <div v-if="!scope.row._isEdit" class="tabel-content">
                  <ToolTip v-if="scope.row.remark && scope.row.remark.length > 20" :content="scope.row.remark">
                    <span class="content">{{ scope.row.remark.substr(0, 20) }}...</span>
                  </ToolTip>
                  <span v-else>{{ scope.row.remark || '--' }}</span>
                </div>
                <div v-if="scope.row._isEdit" class="tabel-input">
                  <!-- <input v-model="scope.row.editRemark" maxlength="500" type="text" > -->
                  <textarea v-model="scope.row.editRemark" maxlength="500" rows="4" />
                </div>
              </template>
            </el-table-column>
            <el-table-column align="left" label="图片" width="218">
              <template slot-scope="scope">
                <div class="tabel-img-box">
                  <div
                    v-for="(img, index) in scope.row.images"
                    :key="index"
                    class="tabel-img-item"
                    @mouseenter="mouseenter"
                    @mouseleave="mouseleave">
                    <img :src="img" class="case-img" alt="">
                    <span class="look-img-mask">
                      <img v-if="!scope.row._isEdit" :src="lookImgIcon" alt="" @click.stop="lookBigImg(img)">
                      <i v-if="scope.row._isEdit" class="el-icon-delete" @click.stop="deleteImg(scope.row, index)" />
                    </span>
                  </div>
                  <UploadImg
                    v-if="scope.row._isEdit && scope.row.images.length < 6"
                    :width="60"
                    :height="60"
                    :medium-img-index="scope.$index"
                    @uploadImgSuccess="uploadImgSuccess"
                  />
                </div>
              </template>
            </el-table-column>
            <el-table-column align="center" label="人员">
              <template slot-scope="scope">
                <div class="tabel-content">{{ scope.row.member }}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" >
              <template slot-scope="scope">
                <div class="flex-center">
                  <div v-if="!scope.row._isEdit" class="button-text" @click="toEditTab(scope.row, true)">编辑</div>
                  <div v-if="scope.row._isEdit" class="button-text" @click="saveEdit(scope.row)">保存</div>
                  <div v-if="scope.row._isEdit" class="button-text bl" @click="toEditTab(scope.row, false, scope.$index)">取消</div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="footer-container">
          <div class="add-btn" @click="addCases">添加<i class="el-icon-plus" /></div>
        </div>
      </div>
    </MaskContainer>
    <LookBigImg v-if="dialogVisible" :img-src="dialogImageUrl" @closed="dialogClosed" />
  </div>
</template>
<script>
import { mapState } from 'vuex';
import MaskContainer from './MaskContainer';
import LookBigImg from './LookBigImg';
import UploadImg from './singleImageUpload';
import lookImgIcon from '@/assets/medium/lookimg.png';
import setting from '@/assets/setting.png';
// filter
import { standardFormatNoHour } from '@/filters/date';

// API
import { editCaseApi } from '@/api/medium';

export default {
  components: { MaskContainer, UploadImg, LookBigImg },
  filters: { standardFormatNoHour },
  props: {},
  data() {
    return {
      lookImgIcon,
      setting,
      dialogVisible: false,
      dialogImageUrl: '',
      maskContainerStyle: {
        width: '960px',
        borderRadius: '12px',
        height: '540px',
        padding: '24px'
      },
      caseList: [],
      listLoading: false,
      mediumId: ''
    };
  },
  computed: {
    ...mapState({
      'userInfo': state => state.user.userInfo
    })
  },
  methods: {
    tabMaskShow(item, noEmitShow = false) {
      this.mediumId = item.mediumId;
      this.currentItem = item;
      const casesList = _.cloneDeep(item.cases);
      casesList.forEach(i => {
        i._isEdit = false;
      });
      this.caseList = casesList || [];
      if (!noEmitShow) {
        this.$refs.MaskContainer.tabMaskShow();
      }
    },
    // 保存修改
    async saveEdit(item) {
      if (item.editRemark === '' || item.editCompany === '') {
        this.$message({
          type: 'error',
          message: item.editCompany === '' ? '品牌不能为空' : '请输入内容'
        });
      } else {
        const params = _.cloneDeep(item);
        params.remark = params.editRemark;
        params.company = params.editCompany;
        params.mediumId = this.mediumId;
        if (params.caseid && params.caseid !== '') params.id = params.caseid;
        delete params.editRemark;
        delete params.editCompany;
        delete params._isEdit;
        const result = await editCaseApi(params);
        if (result.code === 200) {
          // 这边需要更新信息
          item._isEdit = false;
          item.remark = params.remark;
          item.company = params.company;

          this.$emit('mutedUpdateMediumList', params.mediumId);
          this.$message({
            type: 'success',
            message: '保存成功'
          });
        } else {
          this.$message({
            type: 'error',
            message: result.message
          });
        }
      }
    },
    toEditTab(item, type, index) {
      // type为true时是点击编辑，false 是取消
      if (type) {
        // 点击编辑
        item.editRemark = item.remark;
        item.editCompany = item.company;
        // 备份图片
        item.editImages = _.cloneDeep(item.images);
      } else if (!item.caseid) {
        // 点击取消，删除新增的条目
        this.caseList.splice(index, 1);
      } else {
        // 点击取消
        item.images = item.editImages;
      }
      this.caseList.forEach(i => {
        i._isEdit = false;
      });
      item._isEdit = type;
    },
    // 上传图片成功
    uploadImgSuccess({ img, index }) {
      const caseListItem = this.caseList[index];
      caseListItem.images.push(img);
    },
    addCases() {
      const caseList = this.caseList;
      const nowtime = Math.round(new Date().getTime() / 1000);
      const addItem = {
        _isEdit: true,
        time: nowtime,
        member: this.userInfo.userName,
        images: [],
        company: '',
        editCompany: '',
        remark: '',
        editRemark: ''
      };
      // 判断当前是否有还在填写的新增案例
      let flag = true;
      caseList.forEach(i => {
        if (i.company === '') {
          flag = false;
        }
      });
      if (flag) {
        caseList.forEach(i => {
          i._isEdit = false;
        });
        caseList.unshift(addItem);
      }
    },
    // 查看大图
    deleteImg(item, Imgindex) {
      item.images.splice(Imgindex, 1);
    },
    lookBigImg(img) {
      this.dialogVisible = true;
      this.dialogImageUrl = img;
    },
    dialogClosed() {
      this.dialogVisible = false;
      this.dialogImageUrl = '';
    },
    mouseenter(e) {
      const className = e.target.className;
      if (className.indexOf('showLookImg') < 0) {
        e.target.className = e.target.className + ' showLookImg';
      }
    },
    mouseleave(e) {
      const className = e.target.className;
      if (className.indexOf('showLookImg') >= 0) {
        e.target.className = e.target.className.replace(' showLookImg', '');
      }
    }
  }
};
</script>
<style scoped lang="scss">
.remark-container {
  padding: 24px 0 0;
  height: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
  .remark-table-container {
    flex: 1;
    .tabel-content {
      font-size: 14px;
    }
    .tabel-input {
      display: flex;
      input {
        width: 100%;
        height: 100%;
        font-size: 14px;
        text-indent: 6px;
        border:  1px solid #ddd;
        outline: none;
        border-radius: 4px;
      }
      textarea {
        width: 100%;
        height: 100%;
        border:  1px solid #ddd;
        outline: none;
        resize: none;
        font-size: 14px;
        text-indent: 6px;
        line-height: 1.3;
        border-radius: 4px;
      }
    }
    .tabel-btn-container {
      display: flex;
      align-items: center;
      justify-content: center;
      .tabel-btn {
        color: #406eff;
        font-size: 14px;
        cursor: pointer;
        &.mr-10 {
          margin-right: 10px;
        }
      }
    }
  }
  .tabel-img-box {
    display: flex;
    flex-wrap: wrap;
    .tabel-img-item {
      width: 60px;
      height: 60px;
      border-radius: 2px;
      overflow: hidden;
      position: relative;
      cursor: pointer;
      margin-right: 5px;
      margin-bottom: 5px;
      .case-img {
        object-fit: cover;
        width: 100%;
        height: 100%;
      }
      .look-img-mask {
        position: absolute;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;
        background-color: rgba(0,0,0,.4);
        display: flex;
        align-items: center;
        justify-content: center;
        display: none;
        .el-icon-delete {
          font-size: 14px;
          color: #fff;
          margin: 0 5px;
        }
        img {
          width: 20px;
        }
      }
      &.showLookImg .look-img-mask{
        display: flex;
      }
    }
  }
  .footer-container {
    display: flex;
    .add-btn {
      cursor: pointer;
      width: 120px;
      height: 32px;
      background-color: #406eff;
      border-radius: 4px;
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 14px;
      font-weight: 500;
      margin: 27px auto 0;
      .el-icon-plus {
        font-size: 16px;
        margin-left: 4px;
      }
      &:hover {
        background-color: #1f4cdf;
      }
    }
  }
}
.case-big-img {
  max-width: 500px;
  max-height: 500px;
}
</style>
<style lang="scss">
.edit-case-dialog {
  .el-dialog__header {
    display: none;
  }
  .el-dialog__body {
    padding: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
.remark-container {
  .remark-tabel-cell {
    .cell {
      height: 100%;
    }
  }
  .remark-tabel-header {
    .remark-tabel-header-cell {
      background-color: #F5F5FB !important;
      .cell {
        text-align: center;
      }
    }
    .remark-table-cell {
      border-radius: 8px;
    }
  }
}
</style>
